package com.hahaduo.bee.web.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hahaduo.bee.service.AccountTypeService;
import com.hahaduo.bee.web.form.AccountTypeForm;

@Controller
@RequestMapping("/acctypes")
public class AccountTypeController {

	/**
	 * VIEW PATH
	 */
	private static final String PATH_PREFIX = "accounts_type/";

	@Resource
	private AccountTypeService accountTypeService;

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String getAddAccTypePage(Map<String, Object> model) {
		
		AccountTypeForm accountTypeForm = new AccountTypeForm(); 
		
		accountTypeForm.setList(accountTypeService.getAll(false));

		model.put("accountTypeForm", accountTypeForm);
		return PATH_PREFIX + "add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(@Valid AccountTypeForm accountTypeForm,
			BindingResult result, Map<String, Object> model) {
		
		if (result.hasErrors()) {
			accountTypeForm.setList(accountTypeService.getAll(false));
			return new ModelAndView("add");
		}
		
		accountTypeService.add(accountTypeForm);
		return new ModelAndView("redirect:list?t="+new Date().getTime());
	}

	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String getUpdateCustomerPage(@PathVariable String id, ModelMap model) {

		AccountTypeForm accountTypeForm = new AccountTypeForm();
		accountTypeForm.setList(accountTypeService.getAll(false));
		accountTypeForm.setAccountTypeDto(accountTypeService.get(id));
		
		model.put("accountTypeForm", accountTypeForm);
		return PATH_PREFIX + "update";

	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(@Valid AccountTypeForm accountTypeForm,
			BindingResult result, ModelMap model) {
		if (result.hasErrors()) {
			return new ModelAndView("update");
		}
		accountTypeService.update(accountTypeForm);
		// return to list
		return new ModelAndView("redirect:list?t="+new Date().getTime());
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView delete(@PathVariable String id,
			HttpServletRequest request, ModelMap model) {
		accountTypeService.delete(id);
		// return to list
		return new ModelAndView("redirect:../list?t="+new Date().getTime());

	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(ModelMap model) {

		AccountTypeForm accountTypeForm = new AccountTypeForm(); 
		
		accountTypeForm.setList(accountTypeService.getAll(true));

		model.put("accountTypeForm", accountTypeForm);
		
		return PATH_PREFIX + "list";
	}

}